package com.cgx.service.impl;


import com.cgx.dao.MessageDao;
import com.cgx.dao.SessionDao;
import com.cgx.entity.message.Message;
import com.cgx.service.MessageService;
import com.cgx.service.SessionService;
import com.cgx.service.UserService;
import com.cgx.utils.ResultVoUtils;
import com.cgx.utils.UUIDUtils;
import com.cgx.vo.user.UserInfoVo;
import com.cgx.vo.websocket.MessageVo;
import com.cgx.vo.ResultVo;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author chenguanxin
 * @date 2021-05-25 22:40
 */
@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private SessionService sessionService;

    @Autowired
    private MessageDao messageDao;

    @Autowired
    private UserService userService;

    @Override
    public Map<String, ResultVo> sendMessageToUser(MessageVo messageVo) {
        String sessionId = messageVo.getSessionId();
        String content = messageVo.getContent();
        Integer type = messageVo.getType();
        String fromUserId = messageVo.getFromUserId();
        Message message = Message.getMessage(fromUserId);
        message.setSessionId(sessionId);
        message.setMessageId(UUIDUtils.getUUID());
        message.setFromUserId(fromUserId);
        message.setType(type);
        message.setContent(content);
        messageDao.insert(message);
        messageVo.setMessageId(message.getMessageId());
        sessionService.sendMessageBySessionId(messageVo);
        Map<String, ResultVo> result = new HashMap<>();
        ResultVo successResult = ResultVoUtils.getSuccessResult();
        successResult.setData(message);
        result.put(message.getMessageId(), successResult);
        return result;
    }

    @Override
    public Map<String, ResultVo> saveAll(List<Message> messageList) {
        Map<String, ResultVo> result = new HashMap<>();
        if (CollectionUtils.isNotEmpty(messageList)) {
            for (Message message : messageList) {
                messageDao.save(message);
                MessageVo messageVo = MessageVo.changeMessageVo(message);
                sessionService.sendMessageBySessionId(messageVo);
                ResultVo successResult = ResultVoUtils.getSuccessResult();
                successResult.setData(message);
                result.put(message.getMessageId(), successResult);
            }
        }
        return result;
    }

    @Override
    public List<Message> getMessageBySessionId(String sessionId) {
        List<Message> messageList = new ArrayList<>();
        if (StringUtils.isNotBlank(sessionId)) {
            messageList = messageDao.findAllBySessionIdInAndIsDeleteOrderByCreateTimeDesc(sessionId, 0);
        }
        return messageList;
    }

    @Override
    public Map<String, ResultVo> saveMessage(List<MessageVo> messageList) {
        Map<String, ResultVo> result = new HashMap<>();
        if (CollectionUtils.isNotEmpty(messageList)) {
            for (MessageVo messageVo : messageList) {
                String sessionId = messageVo.getSessionId();
                String content = messageVo.getContent();
                Integer type = messageVo.getType();
                String fromUserId = messageVo.getFromUserId();
                Message message = Message.getMessage(fromUserId);
                message.setSessionId(sessionId);
                message.setMessageId(UUIDUtils.getUUID());
                message.setFromUserId(fromUserId);
                message.setType(type);
                message.setContent(content);
                messageDao.insert(message);
                ResultVo successResult = ResultVoUtils.getSuccessResult();
                successResult.setData(fillMessageVo(messageVo, message));
                result.put(messageVo.getProcessId(), successResult);
            }
        }
        return result;
    }

    private MessageVo fillMessageVo(MessageVo messageVo, Message message) {
        String fromUserId = messageVo.getFromUserId();
        messageVo.setMessageId(message.getMessageId());
        messageVo.setTime(message.getCreateTime());
        List<UserInfoVo> userInfoList = userService.getUserInfo(Collections.singletonList(fromUserId));
        if (CollectionUtils.isNotEmpty(userInfoList)) {
            UserInfoVo userInfoVo = userInfoList.get(0);
            if (userInfoVo.getUserInfo() != null && StringUtils.isNotBlank(userInfoVo.getUserInfo().getUserName())) {
                messageVo.setFromUserName(userInfoVo.getUserInfo().getUserName());
            }
            if (userInfoVo.getUserInfo() != null && StringUtils.isNotBlank(userInfoVo.getUserInfo().getAvatarUrl())) {
                messageVo.setFromUserAvatarUrl(userInfoVo.getUserInfo().getAvatarUrl());
            }
        }
        return messageVo;
    }
}
